// by Xeno
#include "x_setup.sqf"
private ["_delay","_disabled","_newveh","_startdir","_startpos","_type","_vehicle"];
if (!isServer) exitWith{};

_vehicle = _this select 0;
_delay = _this select 1;
_startpos = position _vehicle;
_startdir = getDir _vehicle;
_type = typeOf _vehicle;

_vehicle setVariable ["d_vec_islocked", if (locked _vehicle) then {true} else {false}];

while {true} do {
	sleep (_delay + random 15);

	_empty = _vehicle call XfGetVehicleEmpty;
	
	if (_empty) then {	
		_disabled = if (damage _vehicle > 0.9) then {true} else {false};
		
		if ((_disabled && _empty) || (_empty && !(alive _vehicle))) then {
			_isitlocked = _vehicle getVariable "d_vec_islocked";
			deletevehicle _vehicle;
			_vehicle = objNull;
			sleep 0.5;
			_vehicle = createVehicle [_type, _startpos, [], 0, "NONE"];
			_vehicle setpos _startpos;
			_vehicle setdir _startdir;
			_vehicle setVariable ["d_vec_islocked", _isitlocked];
			if (_isitlocked) then {_vehicle lock true};
		};
	};
};
